﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Xml.Linq;

namespace ListWorkSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            GetSiteForCopyList();
        }
        static void RustyTry()
        {
            try
            {
                using (SPSite site = new SPSite("http://wyth/sites/demo/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("ExampleList");
                        SPView view = null;
                        if (list == null)
                        {
                            // create the list
                            web.Lists.Add("ExampleList", "An example list", SPListTemplateType.GenericList);

                            // get a reference to the list
                            list = web.Lists["ExampleList"];

                            // add a nullable text field to the list
                            list.Fields.Add("ExampleField", SPFieldType.Text, false);

                            // add the text field to the default view of the list
                            view = list.DefaultView;
                            view.ViewFields.Add("ExampleField");
                            view.Update();

                            // insert a value into the list
                            SPListItem listItem = list.Items.Add();
                            listItem["ExampleField"] = "Hello";
                            listItem.Update();
                        }
                        else
                        {

                            System.Console.WriteLine(list.ItemCount);
                            System.Console.WriteLine(list.Fields.Count);
                            view = list.DefaultView;
                            SPViewFieldCollection collViewFields = view.ViewFields;
                            foreach (string fieldName in collViewFields)
                            {
                                System.Console.WriteLine(fieldName);
                            }

                            foreach (SPListItem item in list.Items)
                            {
                                System.Console.WriteLine(item.Name);
                            }
                            // fields = list.DefaultView.ViewFields;
                            //SPfieldCollection fields = list.DefaultView.ViewFields;
                            //foreach(SPField field in fields) 

                            //foreach (SPField field in list.DefaultView.ViewFields)
                            //{
                            //    if (field.Hidden | field.ReadOnlyField)
                            //    {
                            //        System.Console.WriteLine( field.GetType() + " "+field.InternalName  );
                            //    }
                            //}


                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static void CoolExcel()
        {
            SPList list=null;
            DataTable dataTable=null;
            try
            {
                Console.WriteLine("Site Url: ");
                // string _siteUrl = Console.ReadLine();
                string _siteUrl = "http://wyth/sites/demo/";
                string _listName = "ExampleList";

                if (!string.IsNullOrEmpty(_siteUrl))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(_siteUrl))
                        {
                            if (site != null)
                            {
                                SPWeb web = site.RootWeb;

                                if (web != null)
                                {

                                 

                                    Console.WriteLine("List Name:");
                            
                                    if (!string.IsNullOrEmpty(_listName))
                                    {
                                        list = web.Lists[_listName];

                                        if (list != null)
                                        {
                                            dataTable = new DataTable();

                                            //Adds Columns to SpreadSheet
                                            InitializeExcel(list, dataTable);

                                            string _schemaXML = list.DefaultView.ViewFields.SchemaXml;

                                            if (list.Items != null && list.ItemCount > 0)
                                            {
                                                foreach (SPListItem _item in list.Items)
                                                {
                                                    DataRow dr = dataTable.NewRow();
                                                    foreach (DataColumn _column in dataTable.Columns)
                                                    {
                                                        if (dataTable.Columns[_column.ColumnName] != null && _item[_column.ColumnName] != null)
                                                        {
                                                            dr[_column.ColumnName] = _item[_column.ColumnName].ToString();
                                                        }
                                                    }
                                                    dataTable.Rows.Add(dr);

                                                }
                                            }

                                        }
                                    }

                                    System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();

                                    grid.HeaderStyle.Font.Bold = true;
                                    grid.DataSource = dataTable;
                                    grid.DataBind();

                                    using (StreamWriter streamWriter = new StreamWriter("f:\\" + list.Title + ".xls", false, Encoding.UTF8))
                                    {
                                        using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter))
                                        {
                                            grid.RenderControl(htmlTextWriter);
                                        }
                                    }

                                    Console.WriteLine("File Created");

                               
                                }
                            }
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }

            Console.ReadLine();
        }


        /*
         * 
         * Get the list columns and output to the file
         * 
         */ 

        public static void InitializeExcel(SPList list, DataTable _datatable)
        {
            if (list != null)
            {
                
                string _schemaXML = list.DefaultView.ViewFields.SchemaXml;
                if (list.Items != null && list.ItemCount > 0)
                {
                    foreach (SPListItem _item in list.Items)
                    {
                        foreach (SPField _itemField in _item.Fields)
                        {
                          
                            if (_schemaXML.Contains(_itemField.InternalName))
                            {
                                if (_item[_itemField.InternalName] != null)
                                {
                                   

                                  
                                        SPFieldLookup spl = (SPFieldLookup)_itemField;
                                        SPFieldType fieldType=spl.Type;
                                        //switch (fieldType)
                                        //{
                                        //    case SPFieldType.AllDayEvent:
                                        //        default:
                                        //}
                                    
                                    
                                        
                                  
                        }
                    }
                        }}}}}



        public static void GetSiteForCopyList()
        {
                        SPList list=null;
            DataTable dataTable=null;
            try
            {
                Console.WriteLine("Site Url: ");
                // string _siteUrl = Console.ReadLine();
                string _siteUrl = "http://wyth/sites/demo/";
                string _listName = "ExampleList";

                if (!string.IsNullOrEmpty(_siteUrl))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(_siteUrl))
                        {
                            if (site != null)
                            {
                                SPWeb web = site.RootWeb;

                                if (web != null)
                                {


                                    Console.WriteLine("List Name:");

                                    if (!string.IsNullOrEmpty(_listName))
                                    {
                                        list = web.Lists[_listName];

                                        if (list != null)
                                        {
                                            dataTable = new DataTable();

                                            //Adds Columns to SpreadSheet
                                            CopyAList(list);
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            }   catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }


        }

         public static void CopyAList(SPList list)
        {
            if (list != null)
            {
                /*
                 *Fields that created are basedon the DEFAULT view 
                 */
                string _schemaXML = list.DefaultView.ViewFields.SchemaXml;
                if (list.Items != null && list.ItemCount > 0)
                {
                    foreach (SPListItem _item in list.Items)
                    {

                        XDocument doc = new XDocument();
                        XElement root = new XElement("root");

                        root.Add(new XComment("List Name is" + list.Title));
                       

                        /*
                         * Spin through the fields
                         * 
                         */ 
                        foreach (SPField _itemField in _item.Fields)
                        {
                          
                            if (_schemaXML.Contains(_itemField.InternalName))
                            {
                                if (_item[_itemField.InternalName] != null)
                                {
                                    string title =_itemField.Title;
                                    string internalName = _itemField.InternalName;
                                   


                                    SPFieldType fieldType = _itemField.Type;
                                    root.Add(new XElement (internalName, fieldType.ToString() ));
                             
                                    }
                                }
                            }
                        doc.Add(root);
                        System.Console.WriteLine(doc);

                        }
                    }
                }
            }
        }
    }

